﻿using System;
using System.Xml.Serialization;
using System.Net;
using System.IO;

    // saves objects fields to xml file
    public class SerializeXML
    {
        //the Local System Drive
        private string drive = string.Empty;

        /// <summary>
        /// Serializes an instance of MasterSet into an XML File
        /// </summary>
        /// <param name="set">Populated MasterSet instance</param>
        public void Serialize(MasterSet set)
        {
            drive = Environment.GetFolderPath(Environment.SpecialFolder.System).Substring(0, 2);
            try
            {
                // Write configuration, serialize and save to file 'set.xml' 
                XmlSerializer xs = new XmlSerializer(set.GetType());
                StreamWriter sw = new StreamWriter(drive+@"\ReserveMeLogs\set.xml");              
                xs.Serialize(sw, set);
                sw.Close();
            }
                // if above throws exception write error to 'SerializeError.txt'
            catch (Exception ex)
            {
                StreamWriter sw = new StreamWriter(drive+@"\ReserveMeLogs\SerializeError.txt");
                sw.WriteLine(ex.Data);
                sw.WriteLine(ex.InnerException);
                sw.WriteLine(ex.Message);
                sw.WriteLine(ex.StackTrace);
                sw.Close();
                throw;
            }
            StreamReader sr = new StreamReader(drive+@"\ReserveMeLogs\set.xml");
            string xml = sr.ReadToEnd();
            sr.Close();            
        }
    }
